package com.naizhuai.ui.adapter.business;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.ui.BaseActivity;
import com.naizhuai.ui.components.EditTextMultiLine;
import com.naizhuai.ui.entity.SimpleKeyValueBean;
import com.naizhuai.ui.entity.setting.ThankDetailsData;
import com.naizhuai.util.DeviceUtils;
import com.naizhuai.util.ImageUtils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * 感谢卡展示item
 */
public class ThankCardItemAdapterHolder extends RecyclerView.Adapter {

    private Context mContext;
    private List<ThankDetailsData> thankDetailsList;
    private String nameFormat;
    private SimpleDateFormat sdf;

    public ThankCardItemAdapterHolder(Context mContext, List<ThankDetailsData> thankDetailsList) {
        this.mContext=mContext;
        this.thankDetailsList=thankDetailsList;
        nameFormat = mContext.getString(R.string.send_user);
        String timeFormat = mContext.getString(R.string.time_view_text_format);
        sdf = new SimpleDateFormat(timeFormat, Locale.CHINA);
    }

    @Override
    public int getItemCount() {
        return thankDetailsList.size();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_thank_card_item, parent, false);
        return new ThankCardItemHolder(sView);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ThankCardItemHolder imageHolder = (ThankCardItemHolder) holder;
        imageHolder.contentImg.post(new Runnable() {
            @Override
            public void run() {
                CardView.LayoutParams params = (CardView.LayoutParams) imageHolder.contentImg.getLayoutParams();
                params.height = imageHolder.contentImg.getWidth();
                imageHolder.contentImg.setLayoutParams(params);
                refreshItemData(imageHolder,thankDetailsList.get(position));
            }
        });
    }

    private void refreshItemData(ThankCardItemHolder imageHolder,ThankDetailsData thankDetailsData){
        refreshDataContent(thankDetailsData.getContent(),imageHolder);
        imageHolder.sendUser.setText(String.format(nameFormat, thankDetailsData.getSendUsername()));
        ImageUtils.glideImageViewByUrl(mContext, imageHolder.contentImg, thankDetailsData.getStyle());
        setItemTime(thankDetailsData.getSendTime(),imageHolder);
    }

    private void setItemTime(String time,ThankCardItemHolder imageHolder){
        long timeStamp = Long.valueOf(time);
        String sd = sdf.format(new Date(timeStamp));
        imageHolder.thankSendTime.setText(sd);
    }

    private void refreshDataContent(String thankData,ThankCardItemHolder imageHolder) {
        String thankTxt = mContext.getString(R.string.thank_card_content_txt);
        String content = String.format(thankTxt, thankData);
        SpannableString ss = new SpannableString(content);
        ss.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.details_msg_text_color)), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置前景色为洋红色
        imageHolder.contentTxt.setText(ss);
    }


    public class ThankCardItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        EditTextMultiLine contentTxt;
        ImageView contentImg;
        TextView sendUser,thankSendTime;

        public ThankCardItemHolder(View view) {
            super(view);
            contentTxt = (EditTextMultiLine) view.findViewById(R.id.thank_content);
            contentImg = (ImageView) view.findViewById(R.id.thank_card_img);
            sendUser = (TextView) view.findViewById(R.id.thanks_send_user);
            thankSendTime = (TextView) view.findViewById(R.id.send_time);
        }

        @Override
        public void onClick(View v) {
//            if (mItemClickListener != null)
//                mItemClickListener.onItemClick(v, getAdapterPosition(), ITEM_IMAGE_CONTENT);
        }
    }
}
